<?php
namespace app\admin\controller;
use app\admin\model\Product;
use think\captcha\Captcha;
use think\Controller;
use app\admin\model\User2;
use app\admin\model\User_product;
use think\Db;
use think\Cookie;

class Car extends Controller
{
    public function index ()
    {
        $yzm = new Captcha();
        //登录
        if(input('username')!="" && input('password')!="" ){//&& $yzm->check(input('yzm'))
            //获取用户
            $user=User2::where(['name'=>input('username'),'password'=>input('password')])->find();//Db::name('user')->find(1)   User::find(1)
            if($user){
                session('user_id', $user['id']);
            }else{
                return  $this->error('登陆失败，请检查数据','login/index');
            }


        }else if(empty(session('user_id'))) {
            return  $this->error('登陆失败，请检查数据','login/index');
        }

        //获取商品
        $productsSql = "select product.*,user_product.num,user_product.user_id from product
                          LEFT JOIN user_product on user_product.product_id=product.id and user_product.user_id=".session('user_id')." order by product.id";
        //print_r($productsSql);
        $products=Db::query($productsSql);
        //print_r($products);
        //商品操作
        if(!empty(input('pId')) && !empty(input('uId'))){
            $up=User_product::where(['product_id'=>input('pId'),'user_id'=>input('uId')])->count();
            $data=[
                'product_id'=>input('pId'),
                'user_id'=>input('uId'),
                'num'=>input('num'),
            ];
            if ($up > 0) {
                //删除
                if (input('num') == 0) {
                    User_product::where(['product_id'=>input('pId'),'user_id'=>input('uId')])->delete();

                } else {
                    //修改
                    User_product::where(['product_id'=>input('pId'),'user_id'=>input('uId')])->update($data);
                }
            } else {
                //添加
                User_product::insert($data);
            }
            $productsSql = "select product.*,user_product.num,user_product.user_id from product
                          LEFT JOIN user_product on user_product.product_id=product.id and user_product.user_id=".session('user_id')." order by product.id";
            $products=Db::query($productsSql);
            setcookie("products",serialize($products),time()+86400,"/");
            return $this->redirect('admin/Car/index');
        }
        //删除商品
        if(!empty(input('del_id'))){
            if(input('del_id')=="da"){
                Db::name('product')->delete(true);
            }else{
                Product::where('id',input('del_id'))->delete();
            }
            $productsSql = "select product.*,user_product.num,user_product.user_id from product
                          LEFT JOIN user_product on user_product.product_id=product.id and user_product.user_id=".session('user_id')." order by product.id";
            $products=Db::query($productsSql);
            if(!empty($products)){
                setcookie("products",serialize($products),time()+86400,"/");
            }
        }
        //设置cookie
        setcookie("products",serialize($products),time()+86400,"/");
        $products=unserialize(cookie("products"));
        $this->assign('products', $products);
        $this->assign('uId', session('user_id'));
        return view('/car');
    }
}
